from fastapi import FastAPI

from typing import Optional
# 支持枚举对象
from enum import Enum

# 借助Pydantic来使用标准的Python类型 声明请求体。
from pydantic import BaseModel


app = FastAPI()


@app.get("/")
def welecome():
    return {"Welcome": "Hello from fast api"}

@app.get('/test')
async def test():
    return {"Format":"key- value", "Example":"The value of eaxmple"}
@app.get("/root")
async def root():
    return {"message": "You are not allowed to access this page",}

@app.get("/items/{item_id}")
# 通过Python类型声明，​​​FastAPI​​​ 提供了数据校验功能; 当输入的item_id不是数字时就会主动报错，q是可选参数
async def read_item(item_id: int, q: Optional[str] = None):
    if q:
        return {"item_id": item_id, "q": q}
    else:
        return {"item_id": item_id}


#定义一个对象Item，他有id，name,price,is_offer等多个属性
class Item(BaseModel):
    name: str
    price: float
    is_offer: Optional[bool] = None

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    return {"item_id": item_id,"item_name": item.name, "item_price": item.price}




class ModelName(str, Enum):
    one = "linux1.0"
    two = "redhat2.0"
    three = "Ubuntu22"

@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name == ModelName.one:
        return {"model_name": model_name, "message": "Primary Gen"}

    if model_name.value == "reahat2.0":
        return {"model_name": model_name, "message": "RedHat is the second Gen"}

    return {"model_name": model_name, "message": "You got the True!"}

# ?
@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
    return {"file_path": file_path}

# 指定查询参数的默认值，相当于 ?param1&param2，在示例中，它们具有 skip=0 和 limit=10 的默认值。
# fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
# @app.get("/items/")
# async def read_item(skip: int = 0, limit: int = 2):
#     return fake_items_db[skip : skip + limit]
